home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9771 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  96 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!jodell
  3. From: jodell@netcom.com (Jake Odell)
  4. Subject: Re: Help !!!!! on 'strstr"
  5. Message-ID: <jodellDo77pn.Kuw@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <314703FF.4045@msmail.st.stems.com> <jodellDo7097.Bto@netcom.com>
  9. Date: Wed, 13 Mar 1996 09:00:59 GMT
  10. Sender: jodell@netcom20.netcom.com
  11.  
  12. Jake Odell (jodell@netcom.com) posted:
  13. # kim hai (Kim_Hai.Ng@msmail.st.stems.com) posted:
  14. # # Help !!!!!
  15.  
  16. # # Problem :
  17. # #     I need to locate a string within another string, and the process 
  18. # # must be case insensitive. As far as I know, strstr() only locates the 
  19. # # exact match and is case sensitive.
  20. # #     Does anyone konw of any functions that could allow me to locate 
  21. # # substrings and is case insensitive ?
  22.  
  23. # # Eg.
  24. # # case 1:
  25. # #     string1 = "abc"
  26. # #     string2 = "abcdef" 
  27. # # case 2:
  28. # #     string2 = "Abcdef"
  29. # # case 3:
  30. # #     string3 = "aBcdef"
  31. # # case 4:
  32. # #     string4 = "abCdef"
  33. # # ...
  34. # # ..
  35. # # .
  36.  
  37. # Here is istrstr() that came out of
  38. # a similar thread a while back:
  39.  
  40. # /*
  41. #  * istrstr.c
  42. #  */
  43. # #if defined(DEBUG)
  44. # # include <stdio.h>
  45. # #endif
  46. # #include <ctype.h>
  47.  
  48. # typedef unsigned char uchar;
  49.  
  50. # char *istrstr(s1, s2) char *s1; char *s2;
  51. # {    char    *p, *ret;
  52. #     int        len1, len2, matches;
  53. #     int        c1, c2;
  54.  
  55. #     for (len1 = 0; s1[len1]); len1++);
  56. #     for (len2 = 0; s2[len2]); len2++);
  57.  
  58.     sigh..  some typoes:   ^--  remove these two rparens
  59.  
  60. #     if (len2 > len1) return NULL;
  61. #     p = ret = s1;
  62.  
  63.     and an omission:
  64.  
  65.     matches = 0;  /* needed for DEBUG */
  66.  
  67. #     while (*p)
  68. #     {
  69. # #if defined(DEBUG)        
  70. #         c1 = *p;
  71. #         c2 = s2[matches];
  72. #         printf("%c/%02x %c/%02x", c1, c1, c2, c2);
  73. # #endif
  74. #         if ((c1 = toupper((uchar) *p++))
  75. #         ==  (c2 = toupper((uchar) s2[matches++])))
  76. #         {
  77. # #if defined(DEBUG)
  78. #             printf("\n");
  79. # #endif
  80. #             if (matches == len2) return ret;
  81. #             continue;
  82. #         }
  83. #         ret = p;
  84. #         if (matches = (c1 == toupper((uchar) *s2)) ? 1 : 0) ret--;
  85. # #if defined(DEBUG)
  86. #         printf("%s\n", matches ? " <-rewind s2 match" : "");
  87. # #endif
  88. #     }
  89. #     return s2[matches] ? NULL : ret;
  90. # }
  91.  
  92. and you'll need a definition for NULL.
  93.  
  94. -- 
  95. jake@pantheon.us.com   jodell@netcom.com
  96.